package com.peng.crm.workbench.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.peng.crm.commons.exception.CustomException;
import com.peng.crm.commons.utils.UUIDUtil;
import com.peng.crm.workbench.dao.ContactsDao;
import com.peng.crm.workbench.entity.Contacts;
import com.peng.crm.workbench.entity.Customer;
import com.peng.crm.workbench.service.ContactsService;
import com.peng.crm.workbench.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户(Contacts)表服务实现类
 *
 * @author makejava
 * @since 2022-07-02 19:11:34
 */
@Service("contactsService")
public class ContactsServiceImpl implements ContactsService {
    @Resource
    private ContactsDao contactsDao;

    @Autowired
    private CustomerService customerService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Contacts queryById(String id) {
        return contactsDao.quertyById(id);
    }

    /**
     * 通过ID查询单条数据，找到其中关联的ID对应的值  字典表与用户表
     * @param id
     * @return
     */
    public Contacts selectById(String id){
        return null;
    }

    /**
     * 查询所有数据
     * @return
     */
    @Override
    public List<Contacts> list() {
        return null;
    }

    /**
     * 分页查询
     *
     * @param pageNumber    页码
     * @param pageSize      条数
     * @return 查询结果
     */
    @Override
    public Map<String, Object> listPage(Integer pageNumber, Integer pageSize, Contacts customer) {
        PageHelper.startPage(pageNumber,pageSize);
        PageInfo<Contacts> pageInfo = new PageInfo(contactsDao.select(customer));
        Map<String, Object> pageMap = new HashMap<>();
        pageMap.put("list",pageInfo.getList());
        pageMap.put("total",pageInfo.getTotal());
        return pageMap;
    }

    /**
     * 新增数据
     *
     * @param contacts 实例对象
     * @return 实例对象
     */
    @Override
    @Transactional
    public Boolean insert(Contacts contacts) {
        if(contacts.getCustomerId() == null || "".equals(contacts.getCustomerId())){
            throw new CustomException(-200,"客户姓名不能为空");
        }
        //查询客户名称是否存在
        Customer customer = customerService.listCustomerName(contacts.getCustomerId());
        if (customer == null){
            //设置新建客户的信息
            Customer customer1 = new Customer();
            String uuid = UUIDUtil.getUUID();
            customer1.setId(uuid);
            customer1.setName(contacts.getCustomerId());
            customer1.setOwner(contacts.getOwner());
            customer1.setContactSummary(contacts.getContactSummary());
            customer1.setNextContactTime(contacts.getNextContactTime());
            customer1.setDescription(contacts.getDescription());
            customer1.setAddress(contacts.getAddress());
            //添加一个客户
            customerService.insert(customer1);
            //设置联系人客户ID
            contacts.setCustomerId(uuid);
        }

        return contactsDao.insert(contacts)>0;
    }

    /**
     * 修改数据
     *
     * @param customer 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean update(Contacts customer) {
        return contactsDao.update(customer)>0;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.contactsDao.deleteById(id) > 0;
    }

    /**
     * 删除多条数据
     * @param ids   主键数组
     * @return
     */
    @Override
    public boolean removeBatch(String[] ids) {
        return contactsDao.deletes(ids);
    }
}
